import Badger from "../../flyer/DestructibleFlyer/Badger";
import { Killer } from "../../funcObj/interface/HealthPoint";
import { FacilityType } from "../../info/GameEnum";
import { Location } from "../../info/Predefine";
import GameManager from "../../manager/GameManager";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import DestructibleFacility from "../DestructibleFacility";
import LaunchableFalicity from "../LaunchableFalicity";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BadgerHouse extends LaunchableFalicity {

    badgerCount: number = 0;

    @property(cc.Collider)
    collider: cc.Collider = null;

    creating: boolean = false;

    get count(): number {
        return this.badgerCount;
    }

    protected start(): void {

        this.node.on(cc.Node.EventType.TOUCH_START, () => {
            this.badgerCount = 1;
            this.dispatch(null);
        })
    }

    init(player: Player): void {
        super.init(player);
        this.price = { diamond: 2, oil: 0 };
        player.facilityArr[FacilityType.平头哥之家].push(this);
        this.creating = false;
        //初始无主，两分钟之后开始派遣
        this.badgerCount = 0;
        this.creat();

        this.collider.enabled = false;
        this.scheduleOnce(() => {
            this.collider.enabled = true;
        }, 0.1);
    }

    creat() {
        //正在创建直接退出
        if (this.creating) return;
        this.CreatingProgress.progress = 0;
        this.creating = true;
        this.CreatingProgress.node.active = true;
        cc.tween(this.CreatingProgress)
            .to(this.makingTime, { progress: 1 })
            .call(() => {
                this.badgerCount++;
                this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, 1);
            }).call(() => {
                this.CreatingProgress.node.active = false;
                this.creating = false;
                //创建满3个，无主则退出并全部派出
                if (this.badgerCount < this.facilityInfo.每基地可制造上限) {
                    this.creat();
                }
            })
            .start();
    }

    /**
     * 向目标位置派遣
     * @param target 
     */
    dispatch(target: Location) {
        super.dispatch(target);
        this.badgerCount--;
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
        let badger: cc.Node = NodePoolManager.instance.obtain(NodePoolManager.instance.badger);
        this.dispatchQueue.push({
            target: target, flyer: badger, dispatchFunc: (curr_target: Location) => {
                badger.position = this.node.position;
                badger.parent = GameManager.instance.badgerParent;
                badger.getComponent(Badger).init(this, curr_target);
                return badger;
            }
        })
        this.creat();
    }

    reducedHp(killer: Killer): boolean {
        if (this.player)
            return super.reducedHp(killer);

        this.currentHp = Math.max(0, this.currentHp - killer.domage);
        this.hpProgress.progress = this.currentHp / this.maxHp;
        this.hpProgress.node.active = this.currentHp < this.maxHp;
        if (this.currentHp == 0) {
            this.die(killer);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 派遣全部，目标为当前
     */
    autoDispatchAll() {
        for (let i = 0; i < this.badgerCount; i++) {
            this.dispatch(null);
        }
    }

    die(killer: Killer): void {
        //停止产出平头哥
        this.unscheduleAllCallbacks();
        this.CreatingProgress.unscheduleAllCallbacks();
        this.creating = false;
        this.badgerCount = 0;
        this.player.node.targetOff(this);
        let index = this.player.facilityArr[this.facilityType].findIndex((item) => item == this)
        this.player.facilityArr[this.facilityType].splice(index, 1);

        //有主的就destroy
        if (!this.player.facilityArr[FacilityType.主基地] && killer.player.node.active) {
            //认主
            this.node.emit(DestructibleFacility.EventType.摧毁);
            let index = this.player.facilityArr[this.facilityType].findIndex((item) => item == this)
            this.player.facilityArr[this.facilityType].splice(index, 1);
            cc.Tween.stopAllByTarget(this.CreatingProgress);
            this.CreatingProgress.unscheduleAllCallbacks();
            this.init(killer.player);
        } else {
            super.die(killer);
            this.node.destroy();
        }
    }
}
